using System.IO;

using Microsoft.SharePoint;

using RealDolmen.Helpers;

namespace RealDolmen.SharePoint.Helpers
{
    public class SPStreamHelper
    {
        #region Fetch Methods
        public static Stream GetStream(string path)
        {
            Stream stream = null;

            if (path.Contains("://"))
                stream = GetSharePointStream(path);
            
            if (stream == null)
                stream = StreamHelper.GetStream(path);

            return stream;
        }

        public static Stream GetSharePointStream(string url)
        {
            Stream stream = null;
            try
            {
                using (SPSite site = new SPSite(url))
                {
                    if (site != null)
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            SPFile file = web.GetFile(url);
                            stream = new MemoryStream();
                            StreamHelper.ReadWriteStream(file.OpenBinaryStream(), stream);
                        }
                    }
                }
            }
            catch { }
            return stream;
        }
	    #endregion // Fetch Methods
    }
}
